<template>
  <div>
    <h1>{{ id ? "编辑" : "新增" }}短信模板</h1>

    <el-form ref="form" label-width="80px">
      <el-form-item label-width="200px" label="模板名称">
        <el-input v-model="model.name"></el-input>
      </el-form-item>
      <el-form-item label-width="200px" label="模板内容">
        <el-input v-model="model.mode"></el-input>
      </el-form-item>
      <el-form-item label-width="200px" label="模版CODE">
        <el-input v-model="model.modeid"></el-input>
      </el-form-item>
      <el-form-item label-width="200px" label="变量属性">
        <el-input v-model="model.type"></el-input>
      </el-form-item>
      <el-form-item label-width="200px">
        <el-button type="primary" @click="onSubmit">{{
          id ? "立即修改" : "立即创建"
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {},
  },
  data() {
    return {
      model: {}
    };
  },
  created() {
    this.id && this.findIdModel();
    console.log(this.$http.defaults.baseURL, "");
  },
  watch:{
    model:{
      handler(val){
        if(val.mode){
          let str = val.mode.match(/((\w|-|\s)+)/ig);
          console.log(str)
          val.type =str?String( str):""
        } else{
          val.type = ""
        }
      },
      deep:true //true 深度监听
    }
  },
  methods: {
    load() {
      this.count += 2;
    },
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      // this.model.imageUrl = URL.createObjectURL(file.raw);
      this.$set(this.model, "img", res.url);
    },
    async onSubmit() {
      if (this.id) {
        await this.$http.put(`noteMode/${this.id}`, this.model);
      } else {
        await this.$http.post("noteMode", this.model);
      }
      this.$message({
        message: `操作成功`,
        type: "success",
      });
      this.$router.push("/noteMode/list");
    },
    async findIdModel() {
      const mode = await this.$http.get(`noteMode/${this.id}`);
      console.log(mode);
      this.model = mode.data;
    },
  },
};
</script>

<style>
.infinite-list {
  height: 200px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px !important;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>